package com.qf.admin.controller;

import com.qf.admin.domain.Echarts;
import com.qf.admin.domain.GoodEchart;
import com.qf.admin.domain.GoodsMoney;
import com.qf.admin.domain.newUserNum;
import com.qf.admin.service.IEchartService;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/show")
@CrossOrigin(origins = "*")  //跨域配置
public class eChartsController {

    private IEchartService echartServiceImpl;

    private eChartsController(IEchartService echartServiceImpl) {
        this.echartServiceImpl = echartServiceImpl;
    }

    @GetMapping("/barChart")
    public List<Echarts> getAll() {
        List<Echarts> echarts = echartServiceImpl.getAll();
        for (int i = 0; i < echarts.size(); i++) {
            //获取商品分类的id1
            Integer id = echarts.get(i).getId();
            //传入商品分类id，返回该分类的销售总金额
            List<GoodsMoney> sumMoney = echartServiceImpl.getSumMoney(id);
            BigDecimal total = BigDecimal.valueOf(0.00);
            for (int j = 0; j < sumMoney.size(); j++) {
                int num = sumMoney.get(j).getNum();//订单销售数量
                BigDecimal numStr = new BigDecimal(num);
                BigDecimal money = sumMoney.get(j).getMoney();//订单销售单价
                //total = total + num * money
                total = total.add(money.multiply(numStr));//累计所有订单的销售金额
            }
            echarts.get(i).setMoney(total);
            //数据插入List<Echarts>，意味着本类小商品所有销售额已经统计完，要清零以方便计算下一个商品分类
            total = BigDecimal.valueOf(0.00);
        }
        //遍历查看List<Echarts>，插入的各类数据是否存在
        for (int i = 0; i < echarts.size(); i++) {
            Integer id = echarts.get(i).getId();
            String name = echarts.get(i).getName();
            BigDecimal money = echarts.get(i).getMoney();
            System.out.println("id:" + id + "  name:" + name + "  money:" + money);
        }
        return echarts;
    }

    @GetMapping("/tenGoodsBarChart")
    public List<GoodEchart> getTenGood() {
        //传入查询条数limit
        List<GoodEchart> goodList = echartServiceImpl.getGoodIdAndNum(10);
        String name = "";
        Integer id = null;
        for (int i = 0; i < goodList.size(); i++) {
            //获取商品的id
            id = goodList.get(i).getId();
            //传入商品id，返回该商品的名字
            name = echartServiceImpl.getGoodName(id);
            //将名字存进对象
            goodList.get(i).setName(name);
        }
        //遍历查看List<Echarts>，插入的各类数据是否存在
        for (int i = 0; i < goodList.size(); i++) {
             id = goodList.get(i).getId();
             name = goodList.get(i).getName();
             int num = goodList.get(i).getNum();
            System.out.println("id:" + id + "  name:" + name + "  num:" + num);
        }
        return goodList;
    }

    @GetMapping("/newUserNum/{day}")
    public List<newUserNum> getNewUserNum(@PathVariable("day") Integer day){
        //获取前端传入的day，即查询最近day天的新增用户
        return echartServiceImpl.getNewUserNum(day);
    }
}
